﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Contrib.BSCarouselSlide.Models;
using Orchard.ContentManagement;
using Orchard.Fields;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using System.Globalization;


namespace Contrib.BSCarouselSlide.Drivers {
    public class BSCarouselSlideDriver : ContentPartDriver<BSCarouselSlidePart> {
        
        
        protected override string Prefix
        {
            get { return "BSCarouselSlide"; }
        }


        protected override DriverResult Display(
            BSCarouselSlidePart part, string displayType, dynamic shapeHelper) {
            return ContentShape("Parts_BSCarouselSlide", () => shapeHelper.Parts_BSCarouselSlide(Content: part));
        }

        //GET
        protected override DriverResult Editor(
            BSCarouselSlidePart part, dynamic shapeHelper) {

            return ContentShape("Parts_BSCarouselSlide_Edit",
                () => shapeHelper.EditorTemplate(
                    TemplateName: "Parts/BSCarouselSlide",
                    Model: part,
                    Prefix: Prefix));
        }
        
        //POST
        protected override DriverResult Editor(
            BSCarouselSlidePart part, IUpdateModel updater, dynamic shapeHelper) {

            updater.TryUpdateModel(part, Prefix, null, null);
            return Editor(part, shapeHelper);
        }

        protected override void Importing(BSCarouselSlidePart part, ImportContentContext context) {
            var carouselKey = context.Attribute(part.PartDefinition.Name, "CarouselKey");
            if (!String.IsNullOrWhiteSpace(carouselKey))
            {
                part.CarouselKey = carouselKey;
            }
            var prio = context.Attribute(part.PartDefinition.Name, "Priority");
            int pri=0;
            if ( int.TryParse(prio,out pri) )
                part.Priority=pri;
            
            var txtcls = context.Attribute(part.PartDefinition.Name, "TextClass");
            if (!String.IsNullOrWhiteSpace(txtcls))
            {
                part.TextClass = txtcls;
            }
            var imgcls = context.Attribute(part.PartDefinition.Name, "ImageClass");
            if (!String.IsNullOrWhiteSpace(imgcls))
            {
                part.ImageClass = imgcls;
            }
            var hasImage = context.Attribute(part.PartDefinition.Name, "HasImage");
            bool val = false;
            if (bool.TryParse(hasImage, out val))
            {
                part.HasImage = val;
            }
            var hasText = context.Attribute(part.PartDefinition.Name, "HasText");
            val = false;
            if (bool.TryParse(hasText, out val))
            {
                part.HasText = val;
            }
        }

        protected override void Exporting(BSCarouselSlidePart part, ExportContentContext context) {
            context.Element(part.PartDefinition.Name).SetAttributeValue("CarouselKey", part.CarouselKey);
            context.Element(part.PartDefinition.Name).SetAttributeValue("Priority", part.Priority.ToString());
            context.Element(part.PartDefinition.Name).SetAttributeValue("TextClass", part.TextClass);
            context.Element(part.PartDefinition.Name).SetAttributeValue("ImageClass", part.ImageClass);
            context.Element(part.PartDefinition.Name).SetAttributeValue("HasImage", part.HasImage.ToString(CultureInfo.InvariantCulture));            
            context.Element(part.PartDefinition.Name).SetAttributeValue("HasText", part.HasText.ToString(CultureInfo.InvariantCulture));              
        }
    }
}